﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModuleITDesk.DAL
{
    public class DALUser
    {
        private ModuleITDeskEntities contextDB;
        public DALUser()
        {
            contextDB = new ModuleITDeskEntities();
        }
        public bool InsertUsers(User user)
        {
            try
            {
                var temp = contextDB.Users.FirstOrDefault(u => u.UserName == user.UserName);
                if (temp == null)
                {
                    contextDB.Users.Add(user);
                    contextDB.SaveChanges();
                    return true;
                }
                else
                {
                   return false;
                }
            }
            catch (Exception ex) { return false; }            
        }
        public bool UpdateUsers(User user)
        {
            try
            {
                User existing = contextDB.Users.Find(user.UserID);
                ((IObjectContextAdapter)contextDB).ObjectContext.Detach(existing);
                contextDB.Entry(user).State = EntityState.Modified;
                contextDB.SaveChanges();
                return true;
            }
            catch { return false; }
        }
        public bool DeleteUser(User user)
        {
            try
            {
                User existing = contextDB.Users.Find(user.UserID);
                ((IObjectContextAdapter)contextDB).ObjectContext.Detach(existing);
                contextDB.Entry(user).State = EntityState.Deleted;
                contextDB.SaveChanges();
                return true;
            }
            catch { return false; }
        }
        public User GetUserByID(int id)
        {
            try
            {
                User Users = contextDB.Users.Where(user => user.UserID == id).First();
                return Users != null ? Users : new User();
            }
            catch { return new User(); }
        }
        public bool ChangedPassword(string username,string oldpass,string newpass)
        {
            try
            {
                var temp = contextDB.Users.Where(u => u.UserName == username && u.Password == oldpass).First();
                if (temp != null)
                {
                    User users = new User();
                    users.UserID = temp.UserID;
                    users.UserName = temp.UserName;
                    users.Password = newpass;
                    users.FirstName = temp.FirstName;
                    users.LastName = temp.LastName;
                    users.Email = temp.Email;
                    users.WhichOne = temp.WhichOne;
                    User existing = contextDB.Users.Find(users.UserID);
                    ((IObjectContextAdapter)contextDB).ObjectContext.Detach(existing);
                    contextDB.Entry(users).State = EntityState.Modified;
                    contextDB.SaveChanges();
                    return true;
                }
                else 
                {
                    return false;
                }

            }
            catch { return false; }
        }
    }
}
